<?php
/**
 * SGoodsCategoryJoinController.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/26
 * Time: 22:34
 */
namespace app\common\auto_curd\php_system\controller;

use app\common\auto_curd\php_system\model\SGoodsCategoryJoinModel;
use think\Controller;

abstract class SGoodsCategoryJoinController extends Controller
{
    /**
     * 创建操作
     * @return \think\response\View|void
     */
    public function create()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $addData = [];

            // 自增id
            if(isset($params['id']) && $params['id']!="")
            {
                $addData['id'] = $params['id'];
            }

            // 添加时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $addData['create_ts'] = $params['create_ts'];
            }

            // 商品id
            if(isset($params['goods_id']) && $params['goods_id']!="")
            {
                $addData['goods_id'] = $params['goods_id'];
            }

            // 分类id
            if(isset($params['category_id']) && $params['category_id']!="")
            {
                $addData['category_id'] = $params['category_id'];
            }

            $status = SGoodsCategoryJoinModel::save(null,$addData);
            if(!$status)
            {
                return $this->error('添加失败');
            }
            else
            {
                return $this->success('添加成功',url('update')."?id=".$status);
            }
        }else{
            return view('common/auto_curd/php_system@s_goods_category_join/create');
        }
    }
    
    /**
     * 更新操作
     */
    public function update()
    {
        if($this->request->isPost()){
            $params = $this->request->post();
            $saveData = [];

            // 自增id
            if(isset($params['id']) && $params['id']!="")
            {
                $saveData['id'] = $params['id'];
            }

            // 添加时间
            if(isset($params['create_ts']) && $params['create_ts']!="")
            {
                $saveData['create_ts'] = $params['create_ts'];
            }

            // 商品id
            if(isset($params['goods_id']) && $params['goods_id']!="")
            {
                $saveData['goods_id'] = $params['goods_id'];
            }

            // 分类id
            if(isset($params['category_id']) && $params['category_id']!="")
            {
                $saveData['category_id'] = $params['category_id'];
            }

            $status = SGoodsCategoryJoinModel::save($saveData['id'],$saveData);
            if(!$status)
            {
                return $this->error('更新失败');
            }
            else
            {
                return $this->success('更新成功');
            }
        }else{
            $row = SGoodsCategoryJoinModel::get(['id'=>$this->request->get('id')]);
            return view('common/auto_curd/php_system@s_goods_category_join/update',[
                'row' => $row
            ]);
        }
    }
    /**
     * 获取操作
     */
    public function read()
    {

        $list = SGoodsCategoryJoinModel::paginate([]);
        $pagination = $list->render();
        return view('common/auto_curd/php_system@s_goods_category_join/read',[
            'list' => $list,
            'pagination' => $pagination
        ]);

    }
    /**
     * 删除操作
     */
    public function delete()
    {

        $status = SGoodsCategoryJoinModel::delete($this->request->param('id'));
        if(!$status)
        {
            return $this->error('删除失败');
        }
        else
        {
            return $this->success('删除成功');
        }

    }
    
}